/*
 * Copyright 2013 Hongxin Telecommunication Technologies Co, Ltd.,
 * Wuhan, Hubei, China. All rights reserved.
 */
package com.hxrd.interaction;

import com.hxrd.Luncher;
import com.hxrd.data.Device;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author piqianghua Created on 2017-1-5, 10:14:41
 */
import java.util.Scanner;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class InterActive
{

	private static Log log = LogFactory.getLog(InterActive.class);
	private DBManage db = new DBManage();
	protected static HashMap<Integer, HashMap<Integer, String>> MapData
			= new HashMap<Integer, HashMap<Integer, String>>();
	protected static HashMap<Integer, String> area
			= new HashMap<Integer, String>();
	protected static HashMap<Integer, String> factory
			= new HashMap<Integer, String>();
	protected static HashMap<Integer, String> deviceno
			= new HashMap<Integer, String>();
	protected static HashMap<String, Integer> Iarea
			= new HashMap<String, Integer>();
	protected static HashMap<String, Integer> Ifactory
			= new HashMap<String, Integer>();
	;
		protected static HashMap<String, Integer> Ideviceno
			= new HashMap<String, Integer>();

	public void InterAction(Luncher luncher)
	{
		MapData = db.QueryDB();
		area = MapData.get(1);
		factory = MapData.get(2);
		deviceno = MapData.get(3);
		for(Integer key : area.keySet())
		{
			Iarea.put(area.get(key), key);
		}
		for(Integer key : factory.keySet())
		{
			Ifactory.put(factory.get(key), key);
		}
		for(Integer key : deviceno.keySet())
		{
			Ideviceno.put(deviceno.get(key), key);
		}
		int user;
		Scanner in = new Scanner(System.in);
		boolean mflag = true;
		while(mflag)
		{
			System.out.println("选择(1:参数管理 2:设备管理 3:退出程序)");
			user = NumberCheck(in, 3);
			switch(user)
			{
				//参数管理
				case 1:
					mflag = FirstInterAction(in, mflag);
					break;
				//设备管理
				case 2:
					mflag = SecondInterAction(in, mflag, luncher);
					break;
				//退出程序
				case 3:
					mflag = false;
					MapData.clear();
					area.clear();
					factory.clear();
					deviceno.clear();
					break;
				default:
					break;
			}
		}
	}

	//输入AC地址
	public String InsertAC(Scanner sc)
	{
		String ip = sc.nextLine();
		//AC地址校验
		if(db.ACDEFINE(ip))
		{
			return InsertAC(sc);
		}
		return ip.trim();
	}

	//输入NASIP
	public String InsertNASIP(Scanner sc)
	{
		String NASIP = sc.nextLine();
		if(NASIP.trim().isEmpty())
		{
			System.out.println("输入为空，请重新输入NASIP：");
			return InsertNASIP(sc);
		}
		if(!db.Regex(NASIP, 2))
		{
			return InsertNASIP(sc);
		}
		return NASIP;
	}

	public String ParamLength(Scanner sc, int order)
	{
		String Param = sc.nextLine();
		if(Param.length() > 20)
		{
			System.out.println("输入长度超过20，请重新输入：");
			return ParamLength(sc, order);
		}
		else if(Param.length() < 1)
		{
			System.out.println("输入长度小于1，请重新输入：");
			return ParamLength(sc, order);
		}
		else if(db.ParamRepeat(Param, order))
		{
			System.out.println("表中已有该数据!");
			return null;
		}
		else
		{
			return Param.trim();
		}

	}

	private static int NumberCheck(Scanner sc, int i)
	{
		String option = sc.nextLine();
		if(option.isEmpty())
		{
			switch(i)
			{
				case 0:
					System.out.println("输入为空，请重新输入整数");
					return NumberCheck(sc, i);
				case 3:
					System.out.println("输入为空，请重新输入整数1~3");
					return NumberCheck(sc, i);
				case 5:
					System.out.println("输入为空，请重新输入整数1~5");
					return NumberCheck(sc, i);
				case 8:
					System.out.println("输入为空，请重新输入整数1~8");
					return NumberCheck(sc, i);
				default:
					System.out.println("输入为空，请重新输入整数");
					return NumberCheck(sc, i);
			}
		}
		for(int j = 0; j < option.length(); j++)
		{
			if(option.charAt(j) < '0' || option.charAt(j) > '9')
			{
				switch(i)
				{
					case 0:
						System.out.println("输入非法，请重新输入整数");
						return NumberCheck(sc, i);
					case 3:
						System.out.println("输入非法，请重新输入整数1~3");
						return NumberCheck(sc, i);
					case 5:
						System.out.println("输入非法，请重新输入整数1~5");
						return NumberCheck(sc, i);
					case 8:
						System.out.println("输入非法，请重新输入整数1~8");
						return NumberCheck(sc, i);
					default:
						System.out.println("输入非法，请重新输入整数");
						return NumberCheck(sc, i);
				}
			}

		}
		if(Double.valueOf(option) > 2147483648.0)
		{
			switch(i)
			{
				case 0:
					System.out.println("输入越界，请重新输入整数");
					return NumberCheck(sc, i);
				case 3:
					System.out.println("输入越界，请重新输入整数1~3");
					return NumberCheck(sc, i);
				case 5:
					System.out.println("输入越界，请重新输入整数1~5");
					return NumberCheck(sc, i);
				case 8:
					System.out.println("输入越界，请重新输入整数1~8");
					return NumberCheck(sc, i);
				default:
					System.out.println("输入越界，请重新输入整数");
					return NumberCheck(sc, i);
			}
		}
		return Integer.valueOf(option.trim());
	}

	public boolean FirstInterAction(Scanner in, boolean mflag)
	{
		boolean pflag = true;
		String sql1, sql2;
		while(pflag)
		{
			System.out.
					println("选择(1:添加地市 2:添加厂家 3:添加设备型号 4:返回主菜单 5:退出程序)");
			int param = NumberCheck(in, 5);

			switch(param)
			{
				//添加地市
				case 1:
					System.out.println("请输入地市");
					String Dvalue = ParamLength(in, param);
					if(Dvalue != null)
					{
						sql1
								= "insert into DISTRICT(areaId) values('" + Dvalue + "')";
						sql2
								= "select ID from DISTRICT where areaId=('" + Dvalue + "')";
						area.put(db.ParamInsert(sql1, sql2), Dvalue);
						System.out.println("地市添加成功！");
					}
					break;
				//添加厂家
				case 2:
					System.out.println("请输入厂家：");
					String Fvalue = ParamLength(in, param);
					if(Fvalue != null)
					{
						sql1
								= "insert into FACTORY(venderId) values('" + Fvalue + "')";
						sql2
								= "select ID from FACTORY where venderId=('" + Fvalue + "')";
						factory.put(db.ParamInsert(sql1, sql2), Fvalue);
						System.out.println("厂家添加成功！");
					}
					break;
				//添加设备型号
				case 3:
					System.out.println("请输入设备型号：");
					String DEvalue = ParamLength(in, param);
					if(DEvalue != null)
					{
						sql1
								= "insert into DEVICENO(modelId) values('" + DEvalue + "')";
						sql2
								= "select ID from DEVICENO where modelId=('" + DEvalue + "')";
						deviceno.put(db.ParamInsert(sql1, sql2), DEvalue);
						System.out.println("设备型号添加成功！");
					}
					break;
				//返回主菜单
				case 4:
					pflag = false;
					break;
				//退出程序
				case 5:
					pflag = false;
					mflag = false;
					MapData.clear();
					area.clear();
					factory.clear();
					deviceno.clear();
					break;
				default:
					break;
			}
		}
		return mflag;
	}

	public boolean SecondInterAction(Scanner in, boolean mflag, Luncher luncher)
	{
		BatchImport gather = new BatchImport();
		boolean flag = true;
		while(flag)
		{
			System.out.
					println("选择(1:导入设备 2:增加设备 3:删除设备 4:查询设备 5:导出设备 6:清空设备 7:返回主菜单 8:退出程序)");
			int user = NumberCheck(in, 8);
			switch(user)
			{
				//导入设备
				case 1:
					try
					{
						gather.loadFile();
					}
					catch(Exception e)
					{
						log.info(getTrace(e));
					}
					;
					break;
				//增加设备
				case 2:
					Device item = new Device();
					System.out.println("请输入数字选择地市");
					int length;
					for(Integer key : area.keySet())
					{
						System.out.
								println(key + " " + area.get(key));
					}
					length = area.size();
					int option = NumberCheck(in, 0);
					while(option <= 0 || option > length)
					{
						System.out.println("输入越界，请重新输入： ");
						option = NumberCheck(in, 0);
					}
					item.setAreaId(option);

					System.out.println("请输入数字选择厂家： ");
					for(Integer key : factory.keySet())
					{
						System.out.println(key + " " + factory.
								get(key));
					}
					length = factory.size();
					int optionF = NumberCheck(in, 0);
					while(optionF <= 0 || optionF > length)
					{
						System.out.println("输入越界，请重新输入： ");
						optionF = NumberCheck(in, 0);
					}
					item.setVenderId(optionF);

					System.out.println("请输入数字选择设备型号： ");
					for(Integer key : deviceno.keySet())
					{
						System.out.println(key + " " + deviceno.
								get(key));
					}
					length = deviceno.size();
					int optionDE = NumberCheck(in, 0);
					while(optionDE <= 0 || optionDE > length)
					{
						System.out.println("输入越界，请重新输入： ");
						optionDE = NumberCheck(in, 0);
					}
					item.setModelId(optionDE);

					System.out.println("请输入AC编号：");
					String ACNO = in.nextLine();
					item.setNumberId(ACNO);

					System.out.println("请输入AC地址：");
					item.setIp(InsertAC(in));

					System.out.println("请输入数字选择主备情况：1 主; 2 备");
					int MasterId = NumberCheck(in, 0);
					while(MasterId < 1 || MasterId > 2)
					{
						System.out.println("输入越界，请重新输入： ");
						MasterId = NumberCheck(in, 0);
					}
					item.setMasterId(MasterId);

					System.out.println("请输入NASIP：");
					item.setNasip(InsertNASIP(in));

					System.out.println("请输入用户名：");
					String USERNAME = in.nextLine();
					item.setUsername(USERNAME);

					System.out.println("请输入密码：");
					String PASSWORD = in.nextLine();
					item.setPassword(PASSWORD);

					System.out.println("请输入共同体：");
					String COMMUNITY = in.nextLine();
					if(COMMUNITY.trim().isEmpty())
					{
						item.setCommunity("sbzg4321");
					}
					else
					{
						item.setCommunity(COMMUNITY);
					}

					System.out.println("请输入端口：(输入数字(1到65535))");
					int port = NumberCheck(in, 0);
					while(port < 1 || port > 65535)
					{
						System.out.println("输入越界，请重新输入：");
						port = NumberCheck(in, 0);
					}
					item.setPort(port);

					System.out.
							println("请选择snmp版本：0 1 2 (0代表V1版本，1代表V2版本，2代表V3版本)");
					int SNMPV = NumberCheck(in, 0);
					while(SNMPV < 0 || SNMPV > 2)
					{
						System.out.println("输入越界，请重新输入：");
						SNMPV = NumberCheck(in, 0);
					}
					item.setVersion(SNMPV);

					String sql3
							= "insert into DEVICE (areaId, venderId,modelId, numberId,ip,masterId,nasip,username,password,community,port,version) values('" + item.
							getAreaId() + "','" + item.
							getVenderId() + "','" + item.
							getModelId() + "','" + item.
							getNumberId() + "','" + item.
							getIp()
							+ "','" + item.
							getMasterId() + "','" + item.getNasip() + "','" + item.
							getUsername() + "','"
							+ item.getPassword() + "','" + item.
							getCommunity() + "','"
							+ item.getPort() + "','" + item.
							getVersion() + "')";
					db.ExecuteDB(sql3, user);
					luncher.addDevice(item);
					break;
				//删除设备
				case 3:
					System.out.println("请输入需要删除IP地址");
					String user1;
					user1 = in.nextLine();
					String dsql
							= "delete FROM DEVICE WHERE ip='" + user1.
							trim() + "' ";
					db.ExecuteDB(dsql, user);
					luncher.deleteDevice(user1);
					break;
				//查询设备
				case 4:
					System.out.println("请输入IP地址：");
					String user2;
					user2 = in.nextLine();
					db.QueryDB(user2);
					break;
				//导出设备	
				case 5:
					db.ExportDB();
					break;
				//清空设备
				case 6:
					String qsql = "delete from DEVICE";
					db.ExecuteDB(qsql, user);
					luncher.clearAllDevice();
					break;
				//返回主菜单
				case 7:
					flag = false;
					break;
				//退出程序
				case 8:
					flag = false;
					mflag = false;
					MapData.clear();
					area.clear();
					factory.clear();
					deviceno.clear();
					break;
				default:
					break;
			}
		}
		return mflag;
	}

	public static String getTrace(Throwable ex)
	{
		StringWriter stringWriter = new StringWriter();
		PrintWriter writer = new PrintWriter(stringWriter);
		ex.printStackTrace(writer);
		StringBuffer buffer = stringWriter.getBuffer();
		return buffer.toString();
	}
}
